//
//  ZHNetWorkManger.m
//  ZHNetworkManager_block
//
//  Created by 邹浩 on 15/6/23.
//  Copyright (c) 2015年 蓝鸥. All rights reserved.
//

#import "ZHNetWorkManger.h"
#import <UIKit/UIKit.h>
#import "NSDictionary+ZHFuncation.h"
#import "SVProgressHUD.h"
#import "YYReachability.h"

@interface ZHNetWorkManger ()



@end


@implementation ZHNetWorkManger
//判断网络状态
- (BOOL)fastNetworkIsOK {
    YYReachability *reach = [YYReachability reachabilityWithHostname:@"http://www.baidu.com"];
    return reach.isReachable;
}

//网络请求的实例方法
- (void)requestWithUrlStr:(NSString *)urlStr success:(void (^)(id object))succcess failed:(void (^)(NSError *error))fail
{
    if ([self fastNetworkIsOK]) {
        [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSURL *url = [NSURL URLWithString:urlStr];
            
            NSURLSessionConfiguration *cog = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:cog];
            
            NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSLog(@"%@,%@,%@", data, response, error);
                if (!error) {
                    
                    //数据解析
                    id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                    //如果解析出错
                    if (object) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [SVProgressHUD showSuccessWithStatus:@"加载成功" duration:1];
                            succcess(object);
                            
                        });
                        //回调错误信息，并且跳出这个方法
                        
                        
                    } else {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [SVProgressHUD showErrorWithStatus:@"数据解析失败" duration:1];
                            fail(error);
                            
                        });
                    }
                    //回调解析好的数据
                } else {
                    //            fail(error);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [SVProgressHUD showErrorWithStatus:@"网络请求失败" duration:1];
                        fail(error);
                        
                    });
                }
            }];
            [task resume];
        });

    } else {
        [SVProgressHUD showErrorWithStatus:@"网络未连接" duration:2];
    }
    
}

- (void)requestWithUrlStr:(NSString *)urlStr parms:(NSDictionary *)dict success:(void (^)(id))succcess failed:(void (^)(NSError *))fail
{
   

    if ([self fastNetworkIsOK]) {
        [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSURL *url = [NSURL URLWithString:urlStr];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
            
            NSURLSessionConfiguration *cog = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:cog];
            
            NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSLog(@"%@,%@,%@", data, response, error);
                if (!error) {
                    
                    //数据解析
                    id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                    //如果解析出错
                    if (object) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [SVProgressHUD showSuccessWithStatus:@"加载成功" duration:1];
                            succcess(object);
                            
                        });
                        //回调错误信息，并且跳出这个方法
                    } else {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [SVProgressHUD showErrorWithStatus:@"数据解析失败" duration:1];
                            fail(error);
                            
                        });
                    }
                    //回调解析好的数据
                } else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [SVProgressHUD showErrorWithStatus:@"网络请求失败" duration:1];
                        fail(error);
                    });
                }
            }];
            [task resume];
        });

    } else {
        [SVProgressHUD showErrorWithStatus:@"网络未连接" duration:2];
    }
    
}


@end
